home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / gamekit-1 / GKCollisionGroup.m < prev    next >
Text File  |  1995-06-12  |  1KB  |  52 lines

  1.  
  2. #import <gamekit/gamekit.h>
  3.  
  4. @implementation GKCollisionGroup
  5.  
  6. - init
  7. {
  8.     id ret = [super init];
  9.     actorLists[0] = [[List alloc] init];
  10.     actorLists[1] = [[List alloc] init];
  11.     collisionDetector = [GKCollider new];
  12.     delegate = nil;
  13.     return ret;
  14. }
  15.  
  16. - (int)tag { return tag; }
  17. - setTag:(int)anInt { tag = anInt; return self; }
  18.  
  19. - addActor:anActor toList:(int)number
  20. {
  21.     return [actorLists[number] addObjectIfAbsent:anActor];
  22. }
  23.  
  24. - removeActor:anActor fromList:(int)number
  25. {
  26.     return [actorLists[number] removeObject:anActor];
  27. }
  28.  
  29. - delegate { return delegate; }
  30. - setDelegate:anObject { delegate = anObject; return self; }
  31.  
  32. - calculateCollisions:sender
  33. {    // This algorithm is n*m (i.e. n^2) so don't make both lists large!!!
  34.     int i, j;
  35.     for (i=0; i<[actorLists[0] count]; i++) {
  36.         id actor1 = [actorLists[0] objectAt:i];
  37.         for (j=0; j<[actorLists[1] count]; j++) {
  38.             id actor2 = [actorLists[1] objectAt:j];
  39.             if ([collisionDetector object:actor1 collidesWith:actor2]) {
  40.                 // notify the actors...
  41.                 [actor1 collidedWith:actor2];
  42.                 [actor2 collidedWith:actor1];
  43.                 if ([delegate respondsTo:@selector(actor:collidedWith:)])
  44.                     [delegate actor:actor1 collidedWith:actor2];
  45.             }
  46.         }
  47.     }
  48.     return self;
  49. }
  50.  
  51. @end
  52.